/**
 * Copyright 2011 Alex Vikulov (vikuloff@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.vvip.stockchart.core;

import com.vvip.stockchart.util.GridPainter;
import com.vvip.stockchart.util.IGridLabelsProvider;
import com.vvip.stockchart.util.GridPainter.GridLabelPosition;
import com.vvip.stockchart.util.GridPainter.GridType;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

public class Axis implements IGridLabelsProvider
{
	public enum Side
	{
		LEFT,
		RIGHT,
		TOP,
		BOTTOM
	}		
	
	public interface ILabelFormatProvider
	{
		public String getAxisLabel(Axis sender,double value);
	}
	
	private Side fSide;
	private float fSize;
	private boolean fIsVisible = true;
	
	private int fBackgroundColor = Color.WHITE;
	private int fOutlineColor = Color.BLACK;
	private Paint fPaint = new Paint();
	
	private final AxisRange fAxisRange = new AxisRange();
	
	private AxisRange fGlobalAxisRange = null;
	
	private ILabelFormatProvider fLabelFormatProvider;
	private final Area fParent;
	
	public Axis(Area parent, Side side)
	{
		fParent = parent;
		fSide = side;
		fSize = isHorizontal()?20.0f:50.0f;
	}
	
	public static boolean isHorizontal(Axis.Side side)	
	{
		return side == Side.TOP || side == Side.BOTTOM;
	}
	
	public static boolean isVertical(Axis.Side side)
	{
		return side == Side.LEFT || side == Side.RIGHT;		
	}
	
	public Area getParent()
	{
		return fParent;
	}
	
	public void setLabelFormatProvider(ILabelFormatProvider p)
	{
		fLabelFormatProvider = p;
	}
	
	public ILabelFormatProvider getLabelFormatProvider()
	{
		return fLabelFormatProvider;
	}
	
	public void setGlobalAxisRange(AxisRange range)
	{
		fGlobalAxisRange = range;
	}
	
	public AxisRange getGlobalAxisRange()
	{
		return fGlobalAxisRange;
	}
	
	public AxisRange getAxisRange()
	{
		return fAxisRange;
	}
	
	public AxisRange getAxisRangeOrGlobalAxisRange()
	{
		return (fGlobalAxisRange == null)?fAxisRange:fGlobalAxisRange;
	}
	
	public void draw(RectF clipRect, Canvas c,int linesCount)
	{	
		if(!isVisible()) return;
		
		this.fPaint.reset();
		this.fPaint.setColor(fBackgroundColor);
		
		c.drawRect(clipRect, fPaint);
		
		fPaint.setColor(fOutlineColor);
		GridLabelPosition pos = GridLabelPosition.NEAR;		
		
		switch(this.getSide())
		{
			case LEFT:
			{
				//c.drawLine(clipRect.right-1,clipRect.top,clipRect.right-1,clipRect.bottom,fPaint);
				pos = GridLabelPosition.FAR;
			}
			break;
			case TOP:
			{
				//c.drawLine(clipRect.left,clipRect.bottom-1,clipRect.right,clipRect.bottom-1,fPaint);
				pos = GridLabelPosition.FAR;
			}
			break;
			case RIGHT:
			{
				//c.drawLine(clipRect.left,clipRect.top,clipRect.left,clipRect.bottom,fPaint);
				pos = GridLabelPosition.NEAR;
			}
			break;
		
			case BOTTOM:
			{
				//c.drawLine(clipRect.left,clipRect.top+1,clipRect.right,clipRect.top+1,fPaint);
				pos = GridLabelPosition.NEAR;
			}
			break;
			
		}
	
		GridPainter.paint(clipRect,	c, this.isHorizontal()?GridType.VERTICAL:GridType.HORIZONTAL, 
				linesCount, fPaint, this, pos, 3.0f);		
	}
	
	public float getSize(float sizeIfInvisible)
	{
		return isVisible()?getSize():sizeIfInvisible;
	}
	
	public float getSize()
	{
		return fSize;
	}
	
	public boolean isVertical()
	{
		return isVertical(fSide);		
	}
	
	public boolean isHorizontal()
	{
		return isHorizontal(fSide);
	}
	public Side getSide()
	{
		return fSide;
	}
	public boolean isVisible() 
	{
		return fIsVisible;
	}
	
	public void setVisible(boolean value)
	{
		fIsVisible = value;
	}

	/* (non-Javadoc)
	 * @see org.stockchart.utils.IGridLabelsProvider#getLabel(float, float)
	 */
	public String getLabel(float current, float total) {
		String result = "";
		
		float ratio = current / total;

		double value = Double.NaN;
		
		if(isHorizontal())
			value = this.getAxisRangeOrGlobalAxisRange().getMinViewValueOrAutoValue()+this.getAxisRangeOrGlobalAxisRange().getViewLenght()*ratio;
		else if(isVertical())
			value = this.getAxisRangeOrGlobalAxisRange().getMaxViewValueOrAutoValue()-this.getAxisRangeOrGlobalAxisRange().getViewLenght()*ratio;

		if(null != this.getLabelFormatProvider() )
		{
			result = this.getLabelFormatProvider().getAxisLabel(this, value);
		}
		
		return result;
	}

}
